<template>
    <div id="register">
        <div id="registerPart">
            <div id="title">注册</div>
            <div id="regUsr">
                <i class="el-icon-s-custom"></i>
                <input type="text" v-model="username" placeholder="用户名">
            </div>
            <div id="regPwd">
                <i class="el-icon-lock"></i>
                <input type="password" v-model="password1" placeholder="密码">
            </div>
            <div id="regPwd2">
                <i class="el-icon-lock"></i>
                <input type="password" v-model="password2" placeholder="请确认密码" @keyup.enter="register">
                <div id="demand">密码由6-18位数字、字母或字符混合组成</div>
            </div>
            <div id="regBtn">
                <i class="el-button" @click="register">注册</i>
            </div>
            <div id="other">
                <router-link to="/login">已有账号？登录</router-link>
            </div>
            
        </div>
    </div>
</template>

<script>

import {registerSubmit} from '../network/product'

export default {
    name: 'register',
    data() {
        return {
            username: '',
            password1: '',
            password2: '',
        }
    },
    methods: {
        register(){
            console.log(this.username);
            console.log(this.password1);
            console.log(this.password1);
            if (!this.username || !this.password1 || !this.password2) {
                this.$message({
                message: "用户名或密码不能为空",
                type: "warning"
                });
                return;
            }
            registerSubmit(this.username, this.password1, this.password2)
                .then(res => {
                    console.log(res.data.res);
                    if (res.data.res == 1) {
                        this.$message.error("用户名已存在");
                        return;
                    } else if (res.data.res == 2) {
                        this.$message.error("密码应至少由两种形式的字符组成，长度在6-18位之间");
                        return;
                    } else if (res.data.res == 3) {
                        this.$message.error("两次密码不一致");
                        return;
                    } else {
                        this.$message({
                        message: "注册成功",
                        type: "success"
                        });
                        this.$store.commit("login");
                        this.$store.state.username = this.username
                        this.$router.push('/')
                    }
                })
                // .catch(err => {
                // this.$message.error("注册失败了~请检查网络并再次尝试");
                // return;
                // });
        }
    }
}

</script>

<style scoped>

#register{
    background-color: white;
    padding: 20px;
    background-image: url('../assets/img/loginBG.jpg');
    background-size: 100%;
    background-position: 0px -175px;
}

#registerPart {
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    height: 400px;
    width: 300px;
    font-size: 20px;
    margin-left: 750px;
    border: 1px solid black;
    background-color: rgba(244, 244, 244, 0.2);
}

#regUsr input, #regPwd input, #regPwd2 input{
    line-height: 26px;
    width: 260px;
    outline: none;
    padding-left: 5px;
    font-size: 20px;
}

.el-button {
    font-style: normal;
    width: 200px;
    font-size: 20px;
}

#other p {
    cursor: pointer;
}

#demand {
    font-size: 12px;
    color: red;
}


</style>